package test.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class App {
	
	private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
	
	public static void main(String[] args) {
		App app = new App();
		new Thread() {
			@Override
			public void run() {
				app.m2();
			}
		}.start();
		new Thread() {
			@Override
			public void run() {
				app.m1();
			}
		}.start();
	}
	
	void m1() {
		Lock wk = lock.writeLock();
		wk.lock();
		System.out.println("m1 start...");
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("m1 end...");
		wk.unlock();
	}
	
	void m2() {
		Lock rk = lock.readLock();
		rk.lock();
		System.out.println("m2 start...");
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("m2 end...");
		rk.unlock();
	}
}
